/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.clinicariobamba.Util;

import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpSession;

/**
 *
 * @author xavier
 */
public class AuthorizationListener implements PhaseListener {

    @Override
    public void afterPhase(PhaseEvent event) {
        FacesContext facesContext = event.getFacesContext();
        String currentPage = facesContext.getViewRoot().getViewId();
        boolean isLoginPage = (currentPage.lastIndexOf("login.xhtml") > -1);
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
        Object usuario = session.getAttribute("user");

        if (!isLoginPage && usuario == null) {
            NavigationHandler nH = facesContext.getApplication().getNavigationHandler();
            nH.handleNavigation(facesContext, null, "/login.xhtml");
        } else {
            Object tipoU = session.getAttribute("tusuario");

            boolean isAdminPage = false;
            boolean isMedicoPage = false;
            boolean isPacientePage = false;

            if (currentPage.lastIndexOf("Administracion") > -1) {
                isAdminPage = true;
            } else {
                if (currentPage.lastIndexOf("Profesional_Salud") > -1) {
                    isMedicoPage = true;
                } else if (currentPage.lastIndexOf("Paciente") > -1) {
                    isPacientePage = true;
                }
            }

//            boolean isAdminPage = currentPage.lastIndexOf("Administracion") > -1;
//            boolean isMedicoPage = currentPage.lastIndexOf("Profesional_Salud") > -1;
//            boolean isPacientePage = currentPage.lastIndexOf("Paciente") > -1;
            if (tipoU == "A") {
                if (isMedicoPage || isPacientePage || isLoginPage) {
                    NavigationHandler nH = facesContext.getApplication().getNavigationHandler();
                    //cambiar a la pagina de administracion
                    nH.handleNavigation(facesContext, null, "/faces/Administracion/Administrador.xhtml");
                }
            }
            if (tipoU == "M") {
                if (isAdminPage || isPacientePage || isLoginPage) {
                    NavigationHandler nH = facesContext.getApplication().getNavigationHandler();
                    nH.handleNavigation(facesContext, null, "/faces/Profesional_Salud/Profesional_Salud.xhtml");
                }
            }
            if (tipoU == "P") {
                if (isAdminPage || isMedicoPage || isLoginPage) {
                    NavigationHandler nH = facesContext.getApplication().getNavigationHandler();
                    nH.handleNavigation(facesContext, null, "/faces/Paciente/Paciente.xhtml");
                }
            }
        }
    }

    @Override
    public void beforePhase(PhaseEvent pe) {

    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;

    }

}
